package com.example.permissionservice.listener;

import com.example.permissionservice.entity.User;
import com.example.permissionservice.service.UserRoleService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RocketMQMessageListener(topic = "register-topic", consumerGroup = "role-binder-group")
@Component
public class RoleBindConsumer implements RocketMQListener<User> {
    Logger logger = LoggerFactory.getLogger(RoleBindConsumer.class);

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public void onMessage(User user) {
        try {
            userRoleService.bindDefaultRole(user.getUserId());
            logger.info("绑定默认角色成功");
        } catch (Exception e) {
            // 抛异常让 RocketMQ 重试
            logger.error("[绑定默认角色失败]:",e);
            throw new RuntimeException("绑定默认角色失败", e);

        }
    }
}
